import aiohttp
import asyncio
import json

async def fetch_weather_data(city_code):
    url = f"http://t.weather.itboy.net/api/weather/city/{city_code}"
    async with aiohttp.ClientSession() as session:
        try:
            async with session.get(url) as response:
                if response.status != 200:
                    raise Exception(f"HTTP error! status: {response.status}")
                data = await response.json()
                print('Weather data:', data)
                return data  # 返回获取的天气数据
        except Exception as error:
            print('Could not fetch weather data:', error)

async def get_weather_forecast(city_code):
    try:
        weather_data = await fetch_weather_data(city_code)
        city_info = weather_data['cityInfo']
        city = city_info['city']
        update_time = city_info['updateTime']
        weather_data_main = weather_data['data']
        wendu = weather_data_main['wendu']
        shidu = weather_data_main['shidu']
        quality = weather_data_main['quality']
        ganmao = weather_data_main['ganmao']
        weather_summary = f"当前{city}的天气：温度{wendu}℃，湿度{shidu}，空气质量{quality}。{ganmao}"
        print(weather_summary)
    except Exception as error:
        print('Could not get weather forecast:', error)

# 使用 asyncio 运行主函数
async def main():
    await get_weather_forecast('101180101')

# 运行 asyncio 主循环
if __name__ == '__main__':
    asyncio.run(main())
